import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { CollectionType } from "../domain/CollectionType";
import { collectionTypeApi, keywordApi, Result } from "../backend/server";

const list: CollectionType[] = [];

export const collectionTypeSlice = createSlice({
  name: "collectionType",
  initialState: {
    list,
    pageNo: 1,
    pageSize: 10,
  },
  reducers: {},
  extraReducers: (builders) => {
    builders.addCase(getCollectionType.rejected, (state, action) => {
      console.log(action.payload);
    });
    builders.addCase(getCollectionType.fulfilled, (state, action) => {
      state.list = action.payload;
    });
  },
});

export const getCollectionType = createAsyncThunk<
  CollectionType[],
  Partial<{
    search: string;
    pageNo: number;
    pageSize: number;
  }>,
  {}
>("collectionType/getCollectionType", async (params, action) => {
  return await collectionTypeApi.getCollectionType(params);
});
export const addCollectionType = createAsyncThunk<
  Result<CollectionType>,
  CollectionType,
  {}
>("collectionType/addCollectionType", async (params, action) => {
  return await collectionTypeApi.addCollectionType(params);
});

export const updateCollectionType = createAsyncThunk<
  Result<CollectionType>,
  CollectionType,
  {}
>("collectionType/updateCollectionType", async (params, action) => {
  return await collectionTypeApi.updateCollectionType(params);
});

export const delCollectionType = createAsyncThunk<
  Result<CollectionType>,
  CollectionType["id"],
  {}
>("collectionType/delCollectionType", async (params, action) => {
  return await collectionTypeApi.deleteCollectionType(params);
});

export const collectionTypeReducer = collectionTypeSlice.reducer;
